package hydrap2p.helper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class ProcessOutput extends Thread {
	private InputStream is;
	private List<String> lines;
	private int exitCode;

	public ProcessOutput(InputStream is) {
		this.is = is;
		this.lines = new ArrayList<String>();
		this.setExitCode(1);
	}

	public void run() {
		try {
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			
			String line = null;
			while ((line = br.readLine()) != null) {
				//Cut it if it's just whitespace
				if(!Pattern.matches("\\s*", line)) {
					System.out.println(line);
					lines.add(line);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();  
		}
	}
	
	public List<String> getLines() {
		return lines;
	}

	public void setExitCode(int exitCode) {
		this.exitCode = exitCode;
	}

	public int getExitCode() {
		return exitCode;
	}
}